import { Student } from '../entity/Student'
import { StudentDetail } from '../entity/StudentDetails'
import { ResponseUtil } from '../helpers/ResponseUtil'
import { Controller, Get, Post } from '../metadata'
import { KoaContext } from '../server'

const STU_CREATE_BODY = [
  'name:string',
  'studentNumber:number',
  '?avatar:string',
  'age:number',
  'sex:number',
  'height:number',
  'weight:number',
  'shoeSize:number',
  '?isPayTuition:number',
  'joinSchoolTime:string',
  '?fatherName:string',
  '?motherName:string',
  'guardian:string',
  '?motherPhoneNumber:number',
  '?fatherPhoneNumber:number',
  '?motherWxchatNumber:string',
  '?fatherWechat:string',
  'address:string',
  'schoolType:number',
  'isGainSchoolUniform:number',
  '?remark:string',
]

const STU_UPDATE_BODY = [
  '?name:string',
  '?studentNumber:number',
  '?avatar:string',
  '?age:number',
  'sex:number',
  '?height:number',
  '?weight:number',
  '?shoeSize:number',
  '?isPayTuition:number',
  '?joinSchoolTime:string',
  '?fatherName:string',
  '?motherName:string',
  '?guardian:string',
  '?motherPhoneNumber:number',
  '?fatherPhoneNumber:number',
  '?motherWxchatNumber:string',
  '?fatherWechat:string',
  '?address:string',
  '?schoolType:number',
  '?isGainSchoolUniform:number',
  '?remark:string',
]

const STU_DETAIL_BODY = [
  // 数组序列化
  'picturePhotos:string',
  '?pictureVideos:string',
  // 数组序列化
  '?evaluate:string',
]

@Controller('/student')
export class StudentController {

  @Post('/create', { query: [], body: STU_CREATE_BODY })
  public async create(ctx: KoaContext<{}, Student>) {
    const { mysql: { studentService } } = ctx
    const { msg, code, id } = await studentService.createStudent(ctx.request.body!)
    if (msg && code !== 0) {
      return ResponseUtil.fail(code, msg)
    }
    return ResponseUtil.success({ data: { id } })
  }

  @Get('/list', { query: ['?offset:number', '?count:number', ], body: [] })
  public async getList(ctx: KoaContext<{ offset: number, count: number }>): Promise<ApiTypes.ApiBaseResult> {
    let { offset, count } = ctx.query
    if (!offset) {
      offset = 0
    }
    if (!count) {
      count = 10
    }
    const { mysql: { studentService } } = ctx
    const { code, list, msg } = await studentService.studentList(offset, count)

    if (msg && code !== 0) {
      return ResponseUtil.fail(code, msg)
    }
    return ResponseUtil.success({ data: { list } })
  }

  @Get('/one', { query: ['id:number'], body: [] })
  public async getStudent(ctx: KoaContext<{ id: number }>): Promise<ApiTypes.ApiBaseResult> {
    let { id } = ctx.query
    const { mysql: { studentService } } = ctx
    const { code, student, msg } = await studentService.getStudentById(id)
    if (msg && code !== 0) {
      return ResponseUtil.fail(code, msg)
    }
    return ResponseUtil.success({ data: { student } })
  }

  @Post('/update', { query: ['id:number'], body: STU_UPDATE_BODY })
  public async update(ctx: KoaContext<{ id: number }, Partial<Student>>): Promise<ApiTypes.ApiBaseResult> {
    let { id } = ctx.query
    const { mysql: { studentService } } = ctx
    const { code, msg } = await studentService.updateStudentInfo(id, ctx.request.body!)
    if (msg && code !== 0) {
      return ResponseUtil.fail(code, msg)
    }
    return ResponseUtil.success({})
  }

  @Post('/works/create', { query: ['studentId:number'], body: STU_DETAIL_BODY })
  public async createDetail(ctx: KoaContext<{ studentId: number }, StudentDetail>) {
    let { studentId } = ctx.query
    const { mysql: { studentService } } = ctx
    const { msg, code, id } = await studentService.createStudentDetail(studentId, ctx.request.body!)
    if (msg && code !== 0) {
      return ResponseUtil.fail(code, msg)
    }
    return ResponseUtil.success({ data: { id } })
  }

  @Post('/works/list', { query: ['studentId:number'], body: [] })
  public async getStudentDetailList(ctx: KoaContext<{ studentId: number }>) {
    let { studentId } = ctx.query
    const { mysql: { studentService } } = ctx
    const { msg, code, detailList } = await studentService.getStudentDetailById(studentId)
    if (msg && code !== 0) {
      return ResponseUtil.fail(code, msg)
    }
    return ResponseUtil.success({ data: { list: detailList } })
  }
}
